var app = getApp()
var myhost = app.globalData.myhost
Page({
  /**
   * 页面的初始数据
   */
  data: {
    userInfo: wx.getStorageSync('userInfo'),
    userid: '暂未登录',
    isAuth: false,
    coinNumber: 0,
    APPID: 'wxf50cb6f4fa8619a3',
    SECRET: '90cdb352209390305eedaf48af9c924b',
    JSCODE: '',
    isqiandao: false
  },
  onChooseAvatar(e) {
    const {
      avatarUrl
    } = e.detail
    this.setData({
      avatarUrl,
    })
  },
  login: function () {
    if (this.data.isAuth) {
      app.globalData.userCode = ''
      app.globalData.userId = ''
      this.setData({
        isAuth: false,
        coinNumber: 0,
        userid: '暂未登录'
      })
      wx.showToast({
        title: '您已退出登录',
        icon: 'none'
      })
    } else {
      //获取用户code
      let that = this
      wx.login({
        success(res) {
          console.log(res)
          if (res.code) {
            //发起网络请求
            wx.request({
              url: 'https://api.weixin.qq.com/sns/jscode2session?appid=' + that.data.APPID + '&secret=' + that.data.SECRET + '&js_code=' + res.code + '&grant_type=authorization_code',
              success: (res) => {
                app.globalData.userCode = res.data.openid
                console.log(app.globalData.userCode)
                //请求用户id
                wx.request({
                  url: myhost + '/consumer/login?username=' + app.globalData.userCode + '&password=' + app.globalData.userCode,
                  method: 'POST',
                  success: (res) => {
                    console.log(res.data)
                    app.globalData.userId = res.data.userMsg.id
                    that.setData({
                      userid: res.data.userMsg.id,
                      coinNumber: Number(res.data.userMsg.coinNum)
                    })
                  },
                  fail:(res)=>{
                    wx.request({
                      url: myhost+'/consumer/add?username='+app.globalData.userCode+'&password='+app.globalData.userCode,
                      method:"POST",
                      success:(res)=>{
                        console.log(res.data)
                      }
                    })
                  }
                })
              }
            })
          } else {
            console.log('登录失败！' + res.errMsg)
          }
        }
      })

      //获取用户名和头像
      wx.getUserProfile({
        desc: '登录',
        success: result => {
          if (result.errMsg === 'getUserProfile:ok') {
            wx.showToast({
              title: '登录成功',
              icon: 'none'
            })
            this.setData({
              userInfo: result.userInfo,
              isAuth: true
            })
          }
        },
        fail: result => {
          wx.showToast({
            title: '已取消登录',
            icon: 'none'
          })
          this.setData({
            isAuth: false,
            coinNumber: 0,
            userid: '暂未登录'
          })
        }
      })
    }
  },
  kefu: function () {
    wx.showToast({
      title: '客服小姐姐还没上班哦',
      icon: 'none'
    })
  },
  qiandao: function () {
    if (this.data.isAuth && !this.data.isqiandao) {
      wx.request({
        url: myhost + '/liu/modifyCoinNum?id=' + this.data.userid + '&coin_num=' + (this.data.coinNumber + 1),
        success: (res) => {
          wx.showToast({
            title: '签到成功',
            icon: 'success'
          })
          this.setData({
            isqiandao: true
          })
        }
      })
    } else if (this.data.isAuth && this.data.isqiandao) {
      wx.showToast({
        title: '今日已签',
        icon: 'error'
      })
    } else {
      wx.showToast({
        title: '请先登录',
        icon: 'error'
      })
    }

  },
  goread: function (e) {
    // wx.showToast({
    //   title: '工程师正在施工',
    //   icon: 'error'
    // })
    if (this.data.isAuth) {
      wx.switchTab({
        url: '/pages/bookshelf/index',
      })
    }else{
      wx.showToast({
        title: '请先登录',
        icon: 'error'
      })
    }
  },
  go: function (e) {
    const {
      page
    } = e.currentTarget.dataset;
    // console.log(page);
    if (page) {
      wx.navigateTo({
        url: page,
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    if (app.globalData.userId) {
      wx.request({
        url: myhost + '/consumer/login?username=' + app.globalData.userCode + '&password=' + app.globalData.userCode,
        method: 'POST',
        success: (res) => {
          console.log(res.data)
          app.globalData.userId = res.data.userMsg.id
          this.setData({
            userid: res.data.userMsg.id,
            coinNumber: res.data.userMsg.coinNum
          })
        }
      })
    }

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {
    // console.log("页面隐藏")

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})